11088 - End up with more teams (DP), 11282 - Mixing invitations & 1481 - Arrange...
[and.git] / Mi manual de algoritmos / version_maraton_interuniversitaria_2008-2 / src / java / priority_queue.tex
blobad9a7ab63c5d071b662833c412d6a19783caa4bc
1 % Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
3 {\ttfamily \raggedright {
4 \noindent
5 \mbox{}\textbf{\textcolor{RoyalBlue}{import}}\ java\textcolor{BrickRed}{.}util\textcolor{BrickRed}{.*;} \\
6 \mbox{} \\
7 \mbox{} \\
8 \mbox{}\textbf{\textcolor{Blue}{class}}\ Item\ \textbf{\textcolor{Blue}{implements}}\ Comparable\textcolor{BrickRed}{$<$}Item\textcolor{BrickRed}{$>$}\textcolor{Red}{\{} \\
9 \mbox{}\ \ \ \ \textcolor{ForestGreen}{int}\ destino\textcolor{BrickRed}{,}\ peso\textcolor{BrickRed}{;} \\
10 \mbox{} \\
11 \mbox{}\ \ \ \ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{ForestGreen}{int}\ destino\textcolor{BrickRed}{,}\ \textcolor{ForestGreen}{int}\ peso\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
12 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{this}}\textcolor{BrickRed}{.}peso\ \textcolor{BrickRed}{=}\ peso\textcolor{BrickRed}{;} \\
13 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{this}}\textcolor{BrickRed}{.}destino\ \textcolor{BrickRed}{=}\ destino\textcolor{BrickRed}{;} \\
14 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
15 \mbox{}\ \ \ \ \textit{\textcolor{Brown}{/*}} \\
16 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ *\ Implementamos\ toda\ la\ javazofia.}} \\
17 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ */}} \\
18 \mbox{}\ \ \ \ \textbf{\textcolor{Blue}{public}}\ \textcolor{ForestGreen}{int}\ \textbf{\textcolor{Black}{compareTo}}\textcolor{BrickRed}{(}Item\ otro\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
19 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{//\ Return\ $<$\ 0\ si\ this\ $<$\ otro}} \\
20 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{//\ Return\ \ \ 0\ si\ this\ ==\ otro}} \\
21 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{//\ Return\ $>$\ 0\ si\ this\ $>$\ otro\ \ }} \\
22 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{return}}\ peso\ \textcolor{BrickRed}{-}\ otro\textcolor{BrickRed}{.}peso\textcolor{BrickRed}{;}\ \textit{\textcolor{Brown}{/*\ Un\ nodo\ es\ menor\ que\ otro\ si\ tiene\ menos\ peso\ */}} \\
23 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
24 \mbox{}\ \ \ \ \textbf{\textcolor{Blue}{public}}\ \textcolor{ForestGreen}{boolean}\ \textbf{\textcolor{Black}{equals}}\textcolor{BrickRed}{(}Object\ otro\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
25 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{if}}\ \textcolor{BrickRed}{(}otro\ \textbf{\textcolor{Blue}{instanceof}}\ Item\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
26 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ Item\ ese\ \textcolor{BrickRed}{=}\ \textcolor{BrickRed}{(}Item\textcolor{BrickRed}{)}otro\textcolor{BrickRed}{;} \\
27 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{return}}\ destino\ \textcolor{BrickRed}{==}\ ese\textcolor{BrickRed}{.}destino\ \textcolor{BrickRed}{\&\&}\ peso\ \textcolor{BrickRed}{==}\ ese\textcolor{BrickRed}{.}peso\textcolor{BrickRed}{;} \\
28 \mbox{}\ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
29 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{return}}\ \textbf{\textcolor{Blue}{false}}\textcolor{BrickRed}{;} \\
30 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
31 \mbox{}\ \ \ \ \textbf{\textcolor{Blue}{public}}\ String\ \textbf{\textcolor{Black}{toString}}\textcolor{BrickRed}{()}\textcolor{Red}{\{} \\
32 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{return}}\ \texttt{\textcolor{Red}{"{}peso\ =\ "{}}}\ \textcolor{BrickRed}{+}\ peso\ \textcolor{BrickRed}{+}\ \texttt{\textcolor{Red}{"{},\ destino\ =\ "{}}}\ \textcolor{BrickRed}{+}\ destino\textcolor{BrickRed}{;} \\
33 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
34 \mbox{}\textcolor{Red}{\}} \\
35 \mbox{} \\
36 \mbox{}\textbf{\textcolor{Blue}{class}}\ Ejemplo\ \textcolor{Red}{\{} \\
37 \mbox{}\ \ \ \ \textbf{\textcolor{Blue}{public}}\ \textbf{\textcolor{Blue}{static}}\ \textcolor{ForestGreen}{void}\ \textbf{\textcolor{Black}{main}}\textcolor{BrickRed}{(}String\textcolor{BrickRed}{[]}\ args\textcolor{BrickRed}{)}\ \textcolor{Red}{\{} \\
38 \mbox{}\ \ \ \ \ \ \ \ PriorityQueue\textcolor{BrickRed}{$<$}Item\textcolor{BrickRed}{$>$}\ q\ \textcolor{BrickRed}{=}\ \textbf{\textcolor{Blue}{new}}\ PriorityQueue\textcolor{BrickRed}{$<$}Item\textcolor{BrickRed}{$>$();} \\
39 \mbox{}\ \ \ \ \ \ \ \ q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{Purple}{12}\textcolor{BrickRed}{,}\ \textcolor{Purple}{0}\textcolor{BrickRed}{));} \\
40 \mbox{}\ \ \ \ \ \ \ \ q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{Purple}{4}\textcolor{BrickRed}{,}\ \textcolor{Purple}{1876}\textcolor{BrickRed}{));} \\
41 \mbox{}\ \ \ \ \ \ \ \ q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{Purple}{13}\textcolor{BrickRed}{,}\ \textcolor{Purple}{0}\textcolor{BrickRed}{));} \\
42 \mbox{}\ \ \ \ \ \ \ \ q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{Purple}{8}\textcolor{BrickRed}{,}\ \textcolor{Purple}{0}\textcolor{BrickRed}{));} \\
43 \mbox{}\ \ \ \ \ \ \ \ q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Item}}\textcolor{BrickRed}{(}\textcolor{Purple}{7}\textcolor{BrickRed}{,}\ \textcolor{Purple}{3}\textcolor{BrickRed}{));} \\
44 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{while}}\ \textcolor{BrickRed}{(!}q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{isEmpty}}\textcolor{BrickRed}{())}\textcolor{Red}{\{} \\
45 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}q\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{poll}}\textcolor{BrickRed}{());} \\
46 \mbox{}\ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
47 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \\
48 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
49 \mbox{}\textcolor{Red}{\}} \\
51 } \normalfont\normalsize